﻿//----------------------------------------------------
//    版权：www.WinnerCMS.com
//    作者：winner2050
//建立时间：2007-03-09
//    描述：查杀被从前台或者其他途径上传过来的web木马
//          作为杀毒软件无视绝大部分web木马的重要补充
//          支持查杀ASP、ASP.Net、PHP、JSP web木马
//----------------------------------------------------
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
namespace WinnerCMS.PlugIn.FileWatcher
{
    /// <summary>
    /// FileWatchScan 的摘要说明
    /// </summary>
    public class FileWatchScan
    {
        FileSystemWatcher fw;
        Main main = new Main();
        CMSConfig config = CMSConfig.Instance();

        #region 启动实时监控
        /// <summary>
        /// 启动实时监控
        /// </summary>
        public void Watch()
        { 
            this.fw = new FileSystemWatcher();
            this.fw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;

            this.fw.Created += new FileSystemEventHandler(Even);
            this.fw.Changed += new FileSystemEventHandler(Even);

            fw.Error += new ErrorEventHandler(fw_Error);
            fw.Disposed += new EventHandler(fw_Disposed);


            fw.Path =HttpServer.MapPath( config.FileWatcher.WatchDir);
            fw.IncludeSubdirectories = true;
            fw.EnableRaisingEvents = true;

            try
            {
                SecurityCenter model = new SecurityCenter();
                model.Remark = " 实时监控引擎启动";
                model.Type = 3;
                SecurityCenterManage.Add(model);
            }
            catch { }
        }
        private void Even(object sender, FileSystemEventArgs e)
        {
            Thread th = new Thread(new ParameterizedThreadStart(Scan));
            th.Start(e.FullPath);
        }

        private void fw_Error(object sender, ErrorEventArgs e)
        {
            SecurityCenter model = new SecurityCenter();
            model.Remark = e.GetException().Message;
            model.Type = 3;
            SecurityCenterManage.Add(model);
        }
        private void fw_Disposed(object sender, EventArgs e)
        {
            try
            {
                SecurityCenter model = new SecurityCenter();
                model.Remark = "实时监控引擎停止工作";
                model.Type = 3;

                SecurityCenterManage.Add(model);
            }
            catch { }

        }
        #endregion

        private void Scan(object FilePath)
        {
            main.ScanSingleFile(FilePath.ToString());
        }
    }
}